// 引用通用查看方案 previewImage组件 交互回调方法

export const mixin = {
	data() {
		return {
			// 预览方案
			shareData: {},
			imgs: [],
			descs: [],
		}
	},
	// 方案分享至微信
	onShareAppMessage(res) {
		console.log("分享方案：", this.shareData)
		if (res.from === 'button') { // 来自页面内分享按钮
			// console.log(res.target)
		}
	
		let imageUrl = this.shareData.content[0][0].photo
		if(imageUrl.indexOf("https://posting.imguangguang.com/") == -1 ) {
			imageUrl = "https://posting.imguangguang.com/" + imageUrl
		}
		
		return {
			title: this.shareData.content[0][0].introduce === '' ? '快来陌搭领取专属你的穿衣搭配方案吧' : this.shareData.content[0][0].introduce,
			path: '/pages/order/placeOrderIndex?wearPlanId=' + this.shareData.id,
			type: 0,
			imageUrl: imageUrl,
		}
	},
	methods: {
		// 方案打开事件
		longPress(data) {
			var _this = this;
			var src = data.src;

			uni.showModal({
				title: '提示',
				content: '是否要保存该方案图片',
				success: function(res) {
					if (res.confirm) {
						uni.authorize({
							scope: 'scope.writePhotosAlbum',
							success() {
								console.log('陌搭图片存储:允许储存');
								_this.downloadImg(src);
							}
						});
					}
				}
			});
		},
		// 修改数据
		changeData(data) {
			this.shareData = data
			// console.log("变化的数据", this.shareData.content)
		},
		//下载并保存文件
		downloadImg(src) {
			//下载图片文件
			uni.showLoading({
				title: '大图提取中'
			});
			uni.downloadFile({
				url: src,
				success: function(res) {
					uni.hideLoading();
					uni.saveImageToPhotosAlbum({
						filePath: res.tempFilePath,
						success: () => {
							uni.showToast({
								title: '已保存至相册',
								duration: 1000
							});
						}
					});
				},
				fail: function() {
					uni.hideLoading();
					uni.showToast({
						title: '图片下载失败',
						icon: 'none',
						duration: 1000
					});
				}
			});
		},
	}
}
